import json
from models import BaseModels
from sqlalchemy import Column, ForeignKey, String, DateTime, BigInteger, Integer


class Serial(BaseModels):
    __tablename__ = 'serials'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    time = Column(DateTime, index=True, nullable=False, comment="采集时间")
    sequence = Column(String(64), nullable=False, index=True, comment="产品序列SN")
    course_id = Column(Integer, ForeignKey("courses.id", ondelete='CASCADE'), index=True, comment="对应总码ID")
    facility_id = Column(Integer, ForeignKey("facility.id", ondelete='CASCADE'), index=True, comment="经过哪台设备")
    aircraft_id = Column(Integer, ForeignKey("aircraft.id", ondelete='CASCADE'), index=True, comment="对应机种ID")
    tasks_id = Column(Integer, ForeignKey("tasks.id", ondelete='CASCADE'), index=True, comment="对应分任务编号ID")
    codes = Column(String(64), nullable=True, index=True, comment="物料总码")
    serial = Column(String(64), nullable=True, index=True, comment="子物料编号")
    name = Column(String(64), nullable=True, comment="物料名称")

    def __repr__(self):
        return json.dumps({
            "serial_id": self.id
        })
